<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="分类">
        <el-cascader style="width: 280px"
                     v-model="queryParams.categoryId"
                     :options="categoryOptions" :props="categoryProps" @change="categoryChange" />
      </el-form-item>
      <el-form-item label="品牌">
        <el-select style="width: 180px" placeholder="请选择" v-model="queryParams.brandId" filterable clearable>
          <el-option
            v-for="item in brands"
            :key="item.brandId"
            :label="item.name"
            :value="item.brandId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select style="width:160px" v-model="queryParams.publishStatus"
                   clearable
                   @keyup.enter.native="handleQuery">
          <el-option label="新建" :value="0"></el-option>
          <el-option label="上架" :value="1"></el-option>
          <el-option label="下架" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键字" prop="key">
        <el-input
          v-model="queryParams.key"
          placeholder="请输入商品关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:spuInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:spuInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:spuInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spuInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="id" header-align="center" align="center" label="id"></el-table-column>
      <el-table-column prop="spuName" header-align="center" align="center" label="名称"></el-table-column>
      <el-table-column prop="spuDescription" header-align="center" align="center" label="描述"></el-table-column>
      <el-table-column prop="categoryId" header-align="center" align="center" label="分类"></el-table-column>
      <el-table-column prop="brandId" header-align="center" align="center" label="品牌"></el-table-column>
      <el-table-column prop="weight" header-align="center" align="center" label="重量"></el-table-column>
      <el-table-column prop="publishStatus" header-align="center" align="center" label="上架状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.publishStatus == 0">新建</el-tag>
          <el-tag v-if="scope.row.publishStatus == 1">已上架</el-tag>
          <el-tag v-if="scope.row.publishStatus == 2">已下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间"></el-table-column>
      <el-table-column prop="updateTime" header-align="center" align="center" label="修改时间"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.publishStatus == 0"
            type="text"
            size="small"
            @click="productUp(scope.row.id)"
          >上架</el-button>
          <el-button type="text" size="small" @click="attrUpdateShow(scope.row)">规格</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改spu信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="spuName">
          <el-input v-model="form.spuName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品描述" prop="spuDescription">
          <el-input v-model="form.spuDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="所属分类id" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入所属分类id" />
        </el-form-item>
        <el-form-item label="品牌id" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入品牌id" />
        </el-form-item>
        <el-form-item label="${comment}" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addSpuInfo, delSpuInfo, getSpuInfo, listSpuInfo, updateSpuInfo, upProduct} from "@/api/product/spuInfo";
import {listCategory} from "@/api/product/category";
import {getBrandsByCategoryId} from '@/api/product/brand'

export default {
  name: "SpuInfo",
  data() {
    return {
      brands:[],
      categoryProps:
        {
          value: 'catId',
          label: 'name',
          children: 'children'
        },
      categoryOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // spu信息表格数据
      spuInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        spuName: null,
        spuDescription: null,
        brandId: null,
        publishStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getCategories();
  },
  methods: {
    productUp(spuId) {
      upProduct(spuId).then(res=>{
        if(res.success){
          this.getDataList();
          this.$message.success("操作成功");
        }else{
          this.$message.error(data.msg);
        }
      });
    },
    attrUpdateShow(row) {
      this.$router.push({
        path: "/product/skuInfo/spec",
        query: { spuId: row.id, categoryId: row.categoryId }
      });
    },
    // 分类树值改变时，获取对应的分组信息
    categoryChange(value) {
      let categoryId = value[value.length - 1];
      this.getBrand(categoryId);
    },
    // 获取分类下品牌信息
    getBrand(categoryId) {
      getBrandsByCategoryId(categoryId).then(response => {
        this.brands = response.data;
      });
    },
    getCategories() {
      listCategory().then(response => {
        this.categoryOptions = this.handleTree(response.data, "catId", "parentCid");
      });
    },
    /** 查询spu信息列表 */
    getList() {
      this.loading = true;
      if(this.queryParams.categoryId && this.queryParams.categoryId instanceof  Array) {
        this.queryParams.categoryId = this.queryParams.categoryId[this.queryParams.categoryId.length - 1];
      }
      listSpuInfo(this.queryParams).then(response => {
        this.spuInfoList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        spuName: null,
        spuDescription: null,
        categoryId: null,
        brandId: null,
        weight: null,
        publishStatus: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加spu信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSpuInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改spu信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSpuInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpuInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除spu信息编号为"' + ids + '"的数据项？').then(function() {
        return delSpuInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/spuInfo/export', {
        ...this.queryParams
      }, `spuInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
